﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BLL;
using Models;
namespace AGVControlSystemPro
{
    public partial class FrmManualCall : Form
    {
        private List<ExCall> objExCall = new List<ExCall>();//创建一个全局变量，用来存储呼叫信息汇总
        public CallInfoImport ImportCallInfo;//声明委托
        public FrmManualCall()
        {
            InitializeComponent();
            //轨道下拉框数据初始化
            this.cboTrackName.DataSource = new BLL.TrackInfoManager().GetAllTrack();
            this.cboTrackName.DisplayMember = "TrackName";
            this.cboTrackName.ValueMember = "TrackId";
            this.cboIP.DataSource = new BLL.RemoteIOManager().GetAllRemoteIO();
            this.cboIP.DisplayMember = "IP";
            this.cboIP.ValueMember = "RemoteName";
            this.dgvCall.AutoGenerateColumns = false;
            AGVInfoManager aGVInfo=new AGVInfoManager();
            var data=aGVInfo.GetAGVip();
            if(data != null)
            {
                this.cboIP.DataSource=data;
            }
        }

        private void FrmManualCall_FormClosed(object sender, FormClosedEventArgs e)
        {
            FrmMain.objFrmManualCall = null;
            //触发委托办法，实现子窗体数据往主窗体传送
            ImportCallInfo(objExCall);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            #region 数据验证
            if (this.cboTrackName.SelectedIndex == -1)
            {
                MessageBox.Show("请选择呼叫所属的轨道", "添加提示");
                return;
            }
            if (this.txtStation1.Text.Trim().Length == 0)
            {
                MessageBox.Show("请输入呼叫站点#1", "添加提示");
                this.txtStation1.Focus();
                return;
            }
            if (this.txtStation2.Text.Trim().Length == 0)
            {
                MessageBox.Show("请输入呼叫站点#2", "添加提示");
                this.txtStation2.Focus();
                return;
            }
            if (this.txtStation3.Text.Trim().Length == 0)
            {
                MessageBox.Show("请输入呼叫站点#3", "添加提示");
                this.txtStation3.Focus();
                return;
            }
            if (!Common.DataValidate.IsPositiveIntegerAndZero(this.txtStation1.Text.Trim()))
            {
                MessageBox.Show("呼叫站点#1必须为正整数", "信息提示");
                this.txtStation1.Focus();
                return;
            }
            if (!Common.DataValidate.IsPositiveIntegerAndZero(this.txtStation2.Text.Trim()))
            {
                MessageBox.Show("呼叫站点#2必须为正整数", "信息提示");
                this.txtStation2.Focus();
                return;
            }
            if (!Common.DataValidate.IsPositiveIntegerAndZero(this.txtStation3.Text.Trim()))
            {
                MessageBox.Show("呼叫站点#3必须为正整数", "信息提示");
                this.txtStation3.Focus();
                return;
            }
            #endregion

            objExCall.Add(new ExCall() 
            {
                TrackName = this.cboTrackName.Text,
                TrackId = Convert.ToInt32(this.cboTrackName.SelectedValue),
                Station1 = Convert.ToInt32(this.txtStation1.Text.Trim()),
                Station2 = Convert.ToInt32(this.txtStation2.Text.Trim()),
                Station3 = Convert.ToInt32(this.txtStation3.Text.Trim()),
                StartTime = Convert.ToDateTime(this.dtCall.Text),
                IP = this.cboIP.Text.Trim(),
                RemoteName = this.cboIP.SelectedValue.ToString()
            });
            this.dgvCall.DataSource = null;
            this.dgvCall.DataSource = objExCall;
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            if (this.dgvCall.CurrentRow == null && this.dgvCall.Rows.Count == 0)
            {
                MessageBox.Show("没有选中需要修改的选型","修改提示");
                return;
            }
            #region 数据验证
            if (this.cboTrackName.SelectedIndex == -1)
            {
                MessageBox.Show("请选择呼叫所属的轨道", "添加提示");
                return;
            }
            if (this.txtStation1.Text.Trim().Length == 0)
            {
                MessageBox.Show("请输入呼叫站点#1", "添加提示");
                this.txtStation1.Focus();
                return;
            }
            if (this.txtStation2.Text.Trim().Length == 0)
            {
                MessageBox.Show("请输入呼叫站点#2", "添加提示");
                this.txtStation2.Focus();
                return;
            }
            if (this.txtStation3.Text.Trim().Length == 0)
            {
                MessageBox.Show("请输入呼叫站点#3", "添加提示");
                this.txtStation3.Focus();
                return;
            }
            if (!Common.DataValidate.IsPositiveIntegerAndZero(this.txtStation1.Text.Trim()))
            {
                MessageBox.Show("呼叫站点#1必须为正整数", "信息提示");
                this.txtStation1.Focus();
                return;
            }
            if (!Common.DataValidate.IsPositiveIntegerAndZero(this.txtStation2.Text.Trim()))
            {
                MessageBox.Show("呼叫站点#2必须为正整数", "信息提示");
                this.txtStation2.Focus();
                return;
            }
            if (!Common.DataValidate.IsPositiveIntegerAndZero(this.txtStation3.Text.Trim()))
            {
                MessageBox.Show("呼叫站点#3必须为正整数", "信息提示");
                this.txtStation3.Focus();
                return;
            }
            #endregion

            int index = this.dgvCall.CurrentRow.Index;//获取索引号
            //数据修改
            this.objExCall[index].TrackName = this.cboTrackName.Text;
            this.objExCall[index].Station1 = Convert.ToInt32(this.txtStation1.Text.Trim());
            this.objExCall[index].Station2 = Convert.ToInt32(this.txtStation2.Text.Trim());
            this.objExCall[index].Station3 = Convert.ToInt32(this.txtStation3.Text.Trim());
            this.objExCall[index].StartTime = this.dtCall.Value;
            this.objExCall[index].IP = this.cboIP.Text;
            this.objExCall[index].RemoteName = this.cboIP.SelectedValue.ToString();
            //数据刷新
            this.dgvCall.DataSource = null;
            this.dgvCall.DataSource = objExCall;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.dgvCall.CurrentRow == null || this.dgvCall.Rows.Count == 0)
            {
                MessageBox.Show("请选择需要删除的数据","删除提示");
                return;
            }
            objExCall.RemoveAt(this.dgvCall.CurrentRow.Index);
            this.dgvCall.DataSource = null;
            this.dgvCall.DataSource = objExCall;
        }

        private void dgvCall_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Common.DataGridViewStyle.DgvRowPostPaint(this.dgvCall,e);
        }

        private void dgvCall_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            this.cboTrackName.Text = this.dgvCall.CurrentRow.Cells["TrackName"].Value.ToString();
            this.txtStation1.Text =this.dgvCall.CurrentRow.Cells["Station1"].Value.ToString();
            this.txtStation2.Text = this.dgvCall.CurrentRow.Cells["Station2"].Value.ToString();
            this.txtStation3.Text = this.dgvCall.CurrentRow.Cells["Station3"].Value.ToString();
            this.dtCall.Value = Convert.ToDateTime(this.dgvCall.CurrentRow.Cells["StartTime"].Value);
            this.cboIP.Text = this.dgvCall.CurrentRow.Cells["IP"].Value.ToString();
        }

        private void FrmManualCall_Load(object sender, EventArgs e)
        {

        }
    }
}
